package org.cp.action;

import java.util.Map;

import org.cp.service.user.IUserService;
import org.cp.vo.User;

import com.opensymphony.xwork.ActionContext;
import com.opensymphony.xwork.ActionSupport;

@SuppressWarnings("serial")
public class UserAction extends ActionSupport {

	protected User user;

	protected IUserService userService;

	public IUserService getUserService() {
		return userService;
	}

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	@SuppressWarnings("unchecked")
	@Override
	public String execute() throws Exception {

		return ERROR;
	}

	public String register() throws Exception {

		try {
			userService.register(user);
		} catch (Exception e) {
			return ERROR;
		}
		return SUCCESS;
	}

	// public String deleteUser() throws Exception {
	//
	// UserService.deleteUser(user);
	// return SUCCESS;
	// }

	public String changeUserInfo() throws Exception {

		userService.changeUserInfo(user);
		return SUCCESS;
	}

	@SuppressWarnings("unchecked")
	public String login() throws Exception {
		User u = userService.login(user.getAccounts(), user
				.getPassword());
		if (u != null) {
			Map session = ActionContext.getContext().getSession();
			session.put("user", u);
			return SUCCESS;
		}

		return ERROR;

	}

	public String logout() throws Exception {

		Map session = ActionContext.getContext().getSession();
		session.remove("testUser");
		return SUCCESS;
	}

}
